package ym.controller.patient;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.Patient;
import ym.service.PatientService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台患者接口
 */
@CrossOrigin
@RestController
@RequestMapping("/back/patient")
public class PatientCon {
    @DubboReference
    private PatientService patientService;

    /**
     * 新增
     *
     * @param patient
     * @return 执行结果
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/patient')")
    public BaseResult add(Patient patient) {
        patientService.add(patient);
        return BaseResult.ok();
    }


    /**
     * 修改
     *
     * @param patient
     * @return 执行结果
     */
    @PostMapping("/update")
    public BaseResult update(Patient patient) {
        patientService.update(patient);
        return BaseResult.ok();
    }


    /**
     * 删除
     *
     * @param patientId
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer patientId) {
        patientService.delete(patientId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询
     *
     * @param patientId
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer patientId) {
        Patient patient = patientService.findById(patientId);
        return BaseResult.ok(patient);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<Patient> all = patientService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询
     *
     * @param search 搜索条件
     * @param page   当前页
     * @param size   每页大小
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/patient')")
    public BaseResult search(String search, int page, int size) {
        Page<Patient> patientPage = patientService.search(search, page, size);
        return BaseResult.ok(patientPage);
    }

    /**
     * 根据治疗阶段查询患者数量
     *
     * @return
     */
    @GetMapping("/findNumberByTreatmentStage")
    public BaseResult findNumberByTreatmentStage(int treatmentStag) {
        int number = patientService.findNumberByTreatmentStage(treatmentStag);
        return BaseResult.ok(number);
    }

    /**
     * 根据住院阶段查询患者数量
     *
     * @return
     */
    @GetMapping("/findNumberByHospitalizationStage")
    public BaseResult findNumberByHospitalizationStage(int hospitalizationStage) {
        int number = patientService.findNumberByHospitalizationStage(hospitalizationStage);
        return BaseResult.ok(number);
    }


}
